﻿using DotNetStar.TheKing.Domain;
using DotNetStar.TheKing.Services;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace DotNetStar.TheKing.Web
{
    public class DetailVideoViewComponent : ViewComponent
    {
        private readonly ContentService _contentService;
        public DetailVideoViewComponent(ContentService contentService)
        {
            _contentService = contentService;
        }

        public async Task<IViewComponentResult> InvokeAsync(Content content, IEnumerable<ContentComment> comments)
        {
            ViewData["Comments"] = comments;

            //热门帖子
            ViewData["HotContents"]= _contentService.GetTopContentQueable(new ContentQuery { Channel = content.Channel })
                .OrderByDescending(m => m.VisitNum)
                .ToList();

            //该帖子用户的其他帖子
            ViewData["UserContents"] = _contentService.GetTopContentQueable(new ContentQuery { Channel = content.Channel })
                .Where(m => m.Author.Id == content.AuthorId && m.Id != content.Id).OrderByDescending(m => m.VisitNum).ToList();

            ViewData["NeedPay"] = false;//是否需要支付
            if (!content.IsFree())
            {
                if (User.Identity.IsAuthenticated)
                {
                    ViewData["NeedPay"] = content.AuthorId != User.Identity.UserId() && !await _contentService.IsContentPaid(content.Id, User.Identity.UserId());
                }
                else
                {
                    ViewData["NeedPay"] = true;
                }
            }
            ViewData["PaidUsers"] = await _contentService.GetPaidUsersAsync(content.Id);

            return View(content);
        }
    }
}
